package com.ruoyi.web.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.kubernetes.models.RoleBindingRequestData;
import com.ruoyi.kubernetes.service.IKubernetesClientRoleBindingService;
import com.ruoyi.kubernetes.service.IKubernetesClientUserService;
import com.ruoyi.web.domain.KubernetesUserRoleBinding;
import com.ruoyi.web.mapper.KubernetesGroupRoleBindingMapper;
import com.ruoyi.web.utils.KubernetesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.KubernetesGroupRoleBinding;
import com.ruoyi.web.mapper.KubernetesGroupMapper;
import com.ruoyi.web.domain.KubernetesGroup;
import com.ruoyi.web.service.IKubernetesGroupService;

/**
 * K8s用户组Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-17
 */
@Service
public class KubernetesGroupServiceImpl implements IKubernetesGroupService
{
    @Autowired
    private KubernetesGroupMapper kubernetesGroupMapper;

    @Autowired
    private KubernetesGroupRoleBindingMapper kubernetesGroupRoleBindingMapper;

    @Autowired
    private IKubernetesClientRoleBindingService kubernetesClientRoleBindingService;
    /**
     * 查询K8s用户组
     *
     * @param id K8s用户组主键
     * @return K8s用户组
     */
    @Override
    public KubernetesGroup selectKubernetesGroupById(Long id)
    {
        return kubernetesGroupMapper.selectKubernetesGroupById(id);
    }

    /**
     * 查询K8s用户组列表
     *
     * @param kubernetesGroup K8s用户组
     * @return K8s用户组
     */
    @Override
    public List<KubernetesGroup> selectKubernetesGroupList(KubernetesGroup kubernetesGroup)
    {
        return kubernetesGroupMapper.selectKubernetesGroupList(kubernetesGroup);
    }

    /**
     * 新增K8s用户组
     *
     * @param kubernetesGroup K8s用户组
     * @return 结果
     */
    @Transactional
    @Override
    public int insertKubernetesGroup(KubernetesGroup kubernetesGroup)
    {
        int rows = kubernetesGroupMapper.insertKubernetesGroup(kubernetesGroup);
        insertKubernetesGroupRoleBinding(kubernetesGroup);
        return rows;
    }

    /**
     * 修改K8s用户组
     *
     * @param kubernetesGroup K8s用户组
     * @return 结果
     */
    @Transactional
    @Override
    public int updateKubernetesGroup(KubernetesGroup kubernetesGroup)
    {
        kubernetesGroupMapper.deleteKubernetesGroupRoleBindingByGroupId(kubernetesGroup.getId());
        insertKubernetesGroupRoleBinding(kubernetesGroup);
        return kubernetesGroupMapper.updateKubernetesGroup(kubernetesGroup);
    }

    /**
     * 批量删除K8s用户组
     *
     * @param ids 需要删除的K8s用户组主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKubernetesGroupByIds(Long[] ids)
    {
        if (ids.length==1){
            List<KubernetesGroupRoleBinding> kubernetesUserRoleBindingList =kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingListByGroupId(ids[0]);
            List<RoleBindingRequestData> roleBindingRequestDataList = new ArrayList();
            kubernetesUserRoleBindingList.forEach(o->{
                roleBindingRequestDataList.add(KubernetesUtils.getRoleBindingRequestData(o));
            });
            if(kubernetesClientRoleBindingService.deleteBindings(roleBindingRequestDataList)) {
                kubernetesGroupMapper.deleteKubernetesGroupRoleBindingByGroupId(ids[0]);
                return kubernetesGroupMapper.deleteKubernetesGroupById(ids[0]);
            }else {
                return 0;
            }
        }
        kubernetesGroupMapper.deleteKubernetesGroupRoleBindingByGroupIds(ids);
        return kubernetesGroupMapper.deleteKubernetesGroupByIds(ids);
    }

    /**
     * 删除K8s用户组信息
     *
     * @param id K8s用户组主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteKubernetesGroupById(Long id)
    {
        List<KubernetesGroupRoleBinding> kubernetesUserRoleBindingList =kubernetesGroupRoleBindingMapper.selectKubernetesGroupRoleBindingListByGroupId(id);
        List<RoleBindingRequestData> roleBindingRequestDataList = new ArrayList();
        kubernetesUserRoleBindingList.forEach(o->{
            roleBindingRequestDataList.add(KubernetesUtils.getRoleBindingRequestData(o));
        });
        if(kubernetesClientRoleBindingService.deleteBindings(roleBindingRequestDataList)) {
            kubernetesGroupMapper.deleteKubernetesGroupRoleBindingByGroupId(id);
            return kubernetesGroupMapper.deleteKubernetesGroupById(id);
        }else {
            return 0;
        }
    }

    /**
     * 新增K8s组权限信息
     *
     * @param kubernetesGroup K8s用户组对象
     */
    public void insertKubernetesGroupRoleBinding(KubernetesGroup kubernetesGroup)
    {
        List<KubernetesGroupRoleBinding> kubernetesGroupRoleBindingList = kubernetesGroup.getKubernetesGroupRoleBindingList();
        Long id = kubernetesGroup.getId();
        if (StringUtils.isNotNull(kubernetesGroupRoleBindingList))
        {
            List<KubernetesGroupRoleBinding> list = new ArrayList<KubernetesGroupRoleBinding>();
            for (KubernetesGroupRoleBinding kubernetesGroupRoleBinding : kubernetesGroupRoleBindingList)
            {
                kubernetesGroupRoleBinding.setGroupId(id);
                list.add(kubernetesGroupRoleBinding);
            }
            if (list.size() > 0)
            {
                kubernetesGroupMapper.batchKubernetesGroupRoleBinding(list);
            }
        }
    }
}
